<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class StocksController extends AppController {

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Stocks','Stocks_details');
	public $components = array('Paginator');
/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 * @throws NotFoundException When the view file could not be found
 *	or MissingViewException in debug mode.
 */
	public function index()
	{
		$stocks = $this->paginate('Stocks');// $this->Newsletters->find('all');
		//var_dump($stocks);
		$this->set('page_title',"LISTADO DE ACCIONES");
		$this->set('stocks',$stocks);	
	}

	public function add()
	{
		$this->set('page_title',"NUEVA ACCIÓN");
		//var_dump($this->data);
		if(!empty($this->data))
		{
			$this->Stocks->set($this->data);
			if($this->Stocks->validates()){
				if($this->Stocks->save($this->data))
				{
					$this->Session->setFlash(__("Se registro exitosamente nuevo Acción ".$this->data['Stocks']['name'], true), 'flash', array('class' => 'muted'));
					$this->redirect(array('action' => 'index'));
				}
				else{		
				$errors = $this->Users->validationErrors;
			    //var_dump($errors);			
				$this->Session->setFlash(__("No se pudo crear nuevo usuario, por favor revisar los campos requeridos.", true), 'flash_error', array('class' => 'alert alert-error'));
				}
			}else{
				$errors = $this->Users->validationErrors;
			    $this->Session->setFlash(__("No se pudo crear nueva Acción, por favor revisar los campos requeridos.", true), 'flash_error', array('class' => 'alert alert-error'));
			}
		}	
	}
	
	public function historical($stock_code){
			
		$stock = $this->Stocks->find('first',array('conditions'=>array('Stocks.code'=>$stock_code)));	
		//var_dump($stock);
		$historical = $this->Stocks_details->find('all',array('conditions'=>array('Stocks_details.stock_id'=>$stock['Stocks']['id'])));
		//$log = $this->Stocks_details->getDataSource()->getLog(false, false);
		//debug($log);
		//var_dump($historical);	
		$this->set('page_title',"Historial de Accion: ".$stock['Stocks']['name']);
		$this->set('stock',$stock);
		$this->set('historical',$historical);	
	}

	/*public function stocks(){
		
		$stocks = $this->paginate('Stocks');// $this->Newsletters->find('all');
		var_dump($stocks);
		$this->set('page_title',"LISTADO DE ACCIONES");
		$this->set('stocks',$stocks);
	}*/
}
